<?php
/**
 * Created by PhpStorm.
 * User: 牛赞赞
 * Date: 2021/2/5
 * Time: 11:11
 */

namespace App\Task;

use Psr\Container\ContainerInterface;
use Swoole\WebSocket\Server as WebSocketServer;

class OutRobotTask
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * 踢出机器人计时器
     * @param $robots
     * @param $room_no
     */
    public function outRobot($robots,$room_no){
        $robot_conf = config('robot');
        $timer      = $robot_conf['out_robot_timer'];
        $redis = redis();
        $out_robot_key = sprintf($robot_conf['user_room_outRobot'],$room_no);
        foreach ($robots as $v){
            sleep($timer);
            if($redis->exists($out_robot_key)){
                $members = $redis->sMembers($out_robot_key);
                if(in_array($v,$members)){
                    // 投递踢出行为计时
                    $task = container()->get(OutRobotTimerTask::class);
                    $task->doOutRobot($v,$room_no);
                }
            }
        }
    }
}